﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;




namespace CoreProcess
{

    public class LongPathSolution
    {
        //private static readonly ILog logger = LogManager.GetLogger(typeof(ResourceScan));

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        //[return: MarshalAs(UnmanagedType.Bool)]
        //internal static extern bool DeleteFile(string lpFileName);
        static extern IntPtr GetShortPathName(string lpszLongPath, string lpszShortPath,IntPtr cchBuffer);

        
        //The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
        //File <260 OK
        //Dir <(248 or 260) ? test result 
        public static string GetShortPathName(string path) {

            //BasicConfigurator.Configure();

            //logger.Debug("debug");
            //logger.Error("error");
            return "";

            string prefixStr = @"\\?\";
            string rr =
            Path.GetDirectoryName(path);

            Directory.EnumerateFiles(@"\\?\"+path);
var rrr=
            Directory.Exists(path);
            Directory.Exists(rr);
            Path.GetDirectoryName(rr);
            string resStr = new string('\0',100);
            if (path.Length >= 260){

                var res =
                GetShortPathName(@"\\?\", resStr, IntPtr.Zero);
            }
                
            return path;
        }

        private string GetShortPathName1(string longPath) {

            string prefixStrPath = @"\\?\" + longPath;

            //get buffer zize
            var res =
            GetShortPathName(prefixStrPath, null, IntPtr.Zero);

            var resStr = new string('\0', res.ToInt32());
            res =
            GetShortPathName(prefixStrPath, resStr, res);

            resStr = resStr.Replace(@"\\?\", "");

            return resStr;
        }
    }
}
